package com.ztools.pc.controller.store;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ztools.common.model.pc.store.Commodity;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.common.Result;
import com.ztools.common.util.pc.TokenAuthorization;
import com.ztools.pc.service.store.CommodityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 一口香菜
 * @Date: 2023/3/26  19:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/store/commodity")
public class CommodityController {

    @Resource
    private CommodityService commodityService;

    @RequestMapping("/list")
    public Result findList(@RequestBody PageUtil<Commodity> pageUtil) {
        Page<Commodity> list = commodityService.findList(pageUtil);
        return list != null ? Result.success("查询成功").data(list) : Result.error();
    }

    @RequestMapping("/delete")
    public Result delete(@RequestBody Commodity commodity) {
        boolean b = commodityService.delete(commodity);
        return b ? Result.success("删除成功") : Result.error();
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Commodity commodity) {
        boolean b = commodityService.save(commodity);
        return b ? Result.success("添加成功") : Result.error();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Commodity commodity) {
        boolean b = commodityService.update(commodity);
        return b ? Result.success("修改成功") : Result.error();
    }

    @GetMapping("/fuzzyQuery/{value}")
    @TokenAuthorization
    public Result fuzzyQuery(@PathVariable("value") String value) {
        if (StrUtil.isNotEmpty(value)) {
            List<Commodity> onlineStockList = commodityService.fuzzyQuery(value);
            return Result.success().data(onlineStockList);
        } else {
            return Result.error("查询内容不能为空！");
        }
    }

    @RequestMapping("/findByObject")
    public Result findByObject(@RequestBody Commodity commodity) {
        List<Commodity> list = commodityService.findByObject(commodity);
        return list != null ? Result.success("查询成功").data(list) : Result.error();
    }
}
